<?php

class Autoload
{
	private static $cache=
	/**** AUTOLOAD CACHE ****/
	array (
  'Autoload' => 'autoload.php',
  'Player' => 'game/player.php',
  'SQL' => 'sql/sql.php',
  'UserAuth' => 'user/auth.php',
);
	/**** AUTOLOAD CACHE END ****/
	
	public static function load($class)
	{
		Controller::requireFile("lib/".self::$cache[$class]);
	}
	public static function build()
	{
		$dir=scandir(".");
		$cache=array();
		foreach($dir as $v)
		{
			if($v!="." && $v!="..")
			{
				if(is_dir($v))
				{
					$subdir=scandir("./".$v);
					foreach($subdir as $w)
					{
						if($w!="." && $w!=".." && is_file($v."/".$w))
						{
							$scan=file_get_contents($v."/".$w);
							preg_match("/class (.*?)\\r\\n\{/", $scan, $matches);
							$cache[$matches[1]]=$v."/".$w;
						}
					}
				}
				elseif(is_file($v))
				{
					$scan=file_get_contents($v);
					preg_match("/class (.*?)\\n\{/", $scan, $matches);
					$cache[$matches[1]]=$v;
				}
			}
		}
		$cache=var_export($cache, 1);
		$f=file_get_contents(__FILE__);
		$f=preg_replace("/\/\*\*\*\* AUTOLOAD CACHE \*\*\*\*\/\\r\\n\\t(.*?)\\r\\n\\t\/\*\*\*\* AUTOLOAD CACHE END \*\*\*\*\//", "/**** AUTOLOAD CACHE ****/\r\n\t".$cache.";\r\n\t/**** AUTOLOAD CACHE END ****/", $f);
		file_put_contents(__FILE__, $f);
	}
}